package com.aaa.controller.fpa;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.aspect.LogMonitor;
import com.aaa.controller.BaseController;
import com.aaa.entity.Result;
import com.aaa.entity.fpa.QuestionFpa;
import com.aaa.entity.PageSortEntity;
import com.aaa.entity.sds.QuestionSds;
import com.aaa.service.fpa.QuestionFpaService;
import com.github.pagehelper.Page;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 问题表(QuestionFpa)表控制层
 *
 * @author
 * @since 2022-07-08 09:27:08
 */
@RestController
@RequestMapping("questionFpa")
public class QuestionFpaController extends BaseController {

    @Resource
    private QuestionFpaService questionFpaService;

    //从controller转发页面
    /*@RequestMapping("toShowQuestionFpa.do")
    public ModelAndView toShowQuestionFpa(ModelAndView modelAndView) {
        modelAndView.setViewName("fpa/showQuestionFpa");
        return modelAndView;
    }*/

    //分页查询
    @RequestMapping(value = "queryAll.do", method = RequestMethod.GET)
    public Result queryAllUserPage(PageSortEntity pageSortEntity, QuestionFpa questionFpa) {
        Page page = questionFpaService.queryAll(pageSortEntity, questionFpa);
        return success(page.getResult(), page.getTotal());
    }

    //添加
    @LogMonitor
    @PostMapping("insert.do")
    public Result add(QuestionFpa questionFpa) {
        QuestionFpa questionFpaEntity = this.questionFpaService.insert(questionFpa);
        return success(questionFpaEntity);

    }

    //修改
    @LogMonitor
    @RequestMapping("/update.do")
    @ResponseBody
    public Result update(QuestionFpa questionFpa) {
        boolean update = this.questionFpaService.update(questionFpa);
        if (update) {
            return success();
        } else {
            return fail();
        }
    }

    //删除
    @LogMonitor
    @RequestMapping("/delete.do")
    @ResponseBody
    public Result delete(String[] ids) {
        List list = new ArrayList();
        for (String id : ids) {
            list.add(id);
        }
        boolean user1 = questionFpaService.delete(list);
        if (user1) {
            return success();
        } else {
            return fail();
        }
    }

    //添加时查询是否重名
    @ResponseBody
    @GetMapping("repetitionName.do")
    public Result repetitionName(QuestionFpa questionFpa) {
        boolean b = this.questionFpaService.repetitionName(questionFpa);
        if (b) {
            return success("问题可用");
        }
        return fail("问题重复，不可用");
    }
    //ID 唯一性校验
    @ResponseBody
    @RequestMapping("/repetitionID.do")
    public Result repetitionID(QuestionFpa questionFpa) {
        if (ObjectUtil.isEmpty(questionFpa.getId())) {
            return fail("不能为空");
        } else {
            boolean b = questionFpaService.repetitionID(questionFpa);
            if (b) {
                return success("ID可用");
            }
            return fail("ID重复不可用");
        }
    }
}

